//数据库-存入通讯地址
const cloud = require('wx-server-sdk')
const db = cloud.database()
const _ = db.command

exports.main = async (event, context) => {
  try {
    if(event.cmdType == "set"){
      return await db.collection('userInfo').where({
        openid: event.openid
      })
      .update({
        data: {
          addressList: _.set(event.list)
        },
      })
    }
    else{
      return await db.collection('userInfo').where({
        openid: event.openid
      })
      .update({
        data: {
          addressList: _.push({
            key:event.key,
            name:event.name,
            phone:event.phone,
            address:event.address,
            select:event.select
          })
        },
      })
    }
  } catch(e) {
    console.error("database userInfo addressList error!-->")
    console.error(e)
  }
}